<?php 
// Library files
include '../includes/cPlayer.php';
include '../includes/cMember.php';
include '../includes/cDatabase.php';
include '../includes/variables.php';
include '../includes/cDates.php';
include '../includes/cGame.php';
include '../includes/cCup.php';
include '../includes/cPage.php';

define("TD_NAME", "<td width='100' align='center'>");
define("TD_SCORE", "<td width='20' align='center'>");

$cPage = new page("Ericsson Cup: Draw");
echo $cPage->getTitle();
echo $cPage->pageStringStart();
echo $cPage->contentStringStart();

$cDates = new dates;
$currentWeek = $cDates->getCurrentWeek();
//$currentWeek = 40;
$cDatabase = new database();

$connect = $cDatabase->connectToDatabase();
$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
$db = $cDatabase->selectDatabase($connect, $activeDatabase);

$selectArray[0] = '*';
$result = $cDatabase->selectFromDatabase($connect, 'memberlist', $selectArray, NULL, NULL, NULL, NULL);

$numMembers = mysqli_num_rows($result);

$cCup = new cup($numMembers);
$drawDone = $cCup->CheckCupDrawHasHappened();
if ($drawDone == 1)
{
	$numberRounds = $cCup->GetNumberRounds();

	for ($round = 2; $round <= $numberRounds; $round++)
	{
		$previousRound = $round - 1;
		$matchIndex = 0;	// The match index for the next round
		for ($Matches = 0; $Matches < $cCup->cRound[$previousRound]->Matches; $Matches++)
		{
			$teamOne = $cCup->cRound[$previousRound]->cMatch[$Matches]->GetWinner();
			$Matches++;
			$teamTwo = $cCup->cRound[$previousRound]->cMatch[$Matches]->GetWinner();
			
			$cCup->cRound[$round]->CreateMatch($teamOne, $teamTwo, $matchIndex);
			$matchIndex++;
		}
	}

	//for ($t = 1; $t <= $numberRounds; $t++)
	//{
	//	echo "<BR>Round ".$cCup->cRound[$t]->Round. " matches ".$cCup->cRound[$t]->Matches;
	//	for ($s = 0; $s < $cCup->cRound[$t]->Matches; $s++)
	//	{
	//		echo "<BR>".$cCup->cRound[$t]->cMatch[$s]->TeamOne->getMemberName()." v ".$cCup->cRound[$t]->cMatch[$s]->TeamTwo->getMemberName();
	//	}
	//}

	// Draw the Cup table
	echo"<BR><BR><BR>";
	echo "\n<table class='cuptable' cellpadding='0' cellspacing='0'>";
	echo "\n	<tr>";
	for ($round = 1; $round <= $numberRounds; $round++)
	{
		echo "\n		<th bgcolor='#CCCCCC'>".$cCup->cRound[$round]->RoundLabel."</th>";
	}
	echo "\n	</tr>";

	echo "\n		<tr>";

	//for ($round = $numberRounds; $round >= 1; $round--)
	for ($round = 1; $round <= $numberRounds; $round++)
	{
		if (($currentWeek >= ($cCup->cRound[$round-1]->Week+1)) )
		{
		echo "\n			<td>";
		echo "\n				<table border='0' cellpadding='0' cellspacing='0'>";
		for ($match = 0; $match < $cCup->cRound[$round]->Matches; $match++)
		{
			echo "\n					<tr>";
			echo "<td bgcolor='#ADD8E6' height='".(25 * pow(2, ($round-1)))."' align='center'><font size='1' face='Verdana'>";
			echo $cCup->cRound[$round]->cMatch[$match]->TeamOne->getMemberName();
			echo "</font></td>";
			
			echo "\n						<td bgcolor='#ADD8E6' height='".(25 * pow(2, ($round-1)))."' align='center'><font size='1' face='Verdana'>";
			if ($cCup->cRound[$round]->cMatch[$match]->TeamOne->getMemberName() != "BYE")
					echo $cCup->cRound[$round]->cMatch[$match]->TeamOnePoints;
			else
				echo "---";
			echo "</font></td>";
			echo "\n						<td bgcolor='#ADD8E6' height='".(25 * pow(2, ($round-1)))."' align='center'><font size='1' face='Verdana'>";
			echo "\\";
			echo "</font></td>";
			echo "\n					</tr>";
			
			
			
			echo "\n					<tr>";
			echo "<td bgcolor='#CCCCCC' height='".(25 * pow(2, ($round-1)))."' align='center'><font size='1' face='Verdana'>";
			echo $cCup->cRound[$round]->cMatch[$match]->TeamTwo->getMemberName();
			echo "</font></td>";
			
			echo "\n						<td bgcolor='#CCCCCC' height='".(25 * pow(2, ($round-1)))."' align='center'><font size='1' face='Verdana'>";
			if ($cCup->cRound[$round]->cMatch[$match]->TeamTwo->getMemberName() != "BYE")
					echo $cCup->cRound[$round]->cMatch[$match]->TeamTwoPoints;
			else
				echo "---";
			echo "</font></td>";
			echo "\n						<td bgcolor='#CCCCCC' height='".(25 * pow(2, ($round-1)))."' align='center'><font size='1' face='Verdana'>";
			echo "/";
			echo "</font></td>";
			echo "\n					</tr>";
		}
		echo "\n				</table>";
		echo "\n			</td>";
		}
	}
	echo "\n		</tr>";
	echo "\n</table>";
}
else
{
	echo "<BR>The draw for the Ericsson Cup has not yet been done for this season.";
}

echo $cPage->getContentStringEnd();
echo $cPage->getMenu();
echo $cPage->getClear();
echo $cPage->pageStringEnd();
echo $cPage->getFooter();
?> 
